package com.lsh.controller;

import com.github.pagehelper.PageInfo;
import com.lsh.constants.Constants;
import com.lsh.entity.QueryPageBean;
import com.lsh.entity.Result;
import com.lsh.pojo.Question;
import com.lsh.pojo.User;
import com.lsh.service.QuestionBasicService;
import com.lsh.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class QuestionBasicController {
    @Autowired
    private QuestionBasicService questionBasicService;
    @PostMapping("/questionbasic/page")
    public Result findQuestionBasicByPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageInfo<Question> pageInfo = questionBasicService.findQuestionBasicByPage(queryPageBean);
            return new Result(true,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }
    @PostMapping("/addquestion")
    public Result addQuestion(@RequestBody Question question, HttpSession httpSession){
        //前端页面完成度低，所以先手动补全一些数据
        question.setStatus(Constants.QUESTION_PRE_PUBLISH);
        question.setReviewStatus(Constants.QUESTION_PRE_REVIEW);
        question.setCreateDate(DateUtils.parseDate2String(new Date()));
        User user = (User) httpSession.getAttribute(Constants.SESSIONID);
        question.setUserId(user.getId());
        //要用业务层方法
        try {
            questionBasicService.addQuestion(question);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        if (!file.isEmpty()) {
            //获取文件名
            String filename = file.getOriginalFilename();
            //获取后缀名
            String fileExtensionName = StringUtils.getFilenameExtension(filename);
            //获取文件存储路径
            String path = request.getServletContext().getRealPath("/img/upload");
            File file1 = new File(path);
            if (!file1.exists()) {
                file1.mkdirs();
            }
            String fileUUIDName = UUID.randomUUID().toString().replace("-", "").toUpperCase()+"."+fileExtensionName;
            InputStream is = null;
            FileOutputStream os = null;
            try {
                 is = file.getInputStream();
                 os = new FileOutputStream(new File(file1, fileUUIDName));
                 FileCopyUtils.copy(is,os);

                return new Result(true,"图片上传失败","img/upload/"+fileUUIDName);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,"图片上传失败");
            } finally {
                try {
                    os.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new Result(false,"未获取到图片信息");
    }
}
